﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web;

namespace Project.Tool
{
	public class UpFileTool : prototypeFileTool
	{
		/// <summary>
		/// 上传图片
		/// </summary>
		/// <param name="files">HTTP承载文件</param>
		/// <returns></returns>
		public static List<string> UpImg(HttpFileCollectionBase files) {

			upFile upfile = new upFile(ImgExt,FileName,"/Content/Images/");

			return upfile.UpLoadFile(files,ComPress:true);

		}

		public static List<string> UpImg(HttpFileCollection files)
		{
			upFile upfile = new upFile(ImgExt,FileName,"/Content/Images/");

			return upfile.UpLoadFile(files, ComPress: true);
		}

		/// <summary>
		/// 上传视频
		/// </summary>
		/// <param name="files">HTTP文件</param>
		/// <param name="ComPress">是否生成缩略图[true]</param>
		/// <returns></returns>
		public static List<string> UpVideo(HttpFileCollectionBase files, bool ComPress = true)
		{
			upFile upfile = new upFile(VideoExt,FileName,"/Content/Videos/");

			return upfile.UpLoadFile(files, "Vedio", ComPress);
		}

        /// <summary>
		/// 上传文件
		/// </summary>
		/// <param name="files">HTTP文件</param>
		/// <param name="ComPress">是否生成缩略图[true]</param>
		/// <returns></returns>
		public static List<string> UpFile(HttpFileCollectionBase files, bool ComPress = true)
        {

            string[] file = { "file", "files" };

            if (files.Count.IsNot(0))
            {
                var fileNameExt = Path.GetExtension(files[0].FileName).ToLower();

                if (ImgExt.Contains(fileNameExt))
                {
                    file[0] = "Image"; file[1] = "Images";
                }
                else if (VideoExt.Contains(fileNameExt))
                {
                    file[0] = "Vedio"; file[1] = "Videos";
                }
                else {
                    ComPress = false;
                }

            }

            upFile upfile = new upFile(new SortedList(), FileName, $"/Content/{file[1]}/");

            return upfile.UpLoadFile(files, file[0], ComPress);
        }
    }
	public class prototypeFileTool {

		/// <summary>
		/// IMG文件扩展名
		/// </summary>
		protected static SortedList ImgExt
		{
			get
			{
				if ( ApplicationHelper.Get("ImgExt") == null )
				{
					string[]
					allowExt = new string[] { ".gif", ".jpg", ".jpeg", ".rar",".zip", ".png",".bmp" };

					SortedList
						Model = new SortedList();

					foreach ( var orExt in allowExt ) Model.Add(orExt, orExt);

                    ApplicationHelper.Set("ImgExt", Model);

					return Model;
				}
				else
					return (SortedList)ApplicationHelper.Get("ImgExt");

			}
		}

        /// <summary>
        /// Video 文件后缀名
        /// </summary>
        protected static SortedList VideoExt
		{
			get
			{
				if (ApplicationHelper.Get("VideoExt") == null )
				{
					string[]
					allowExt = new string[] { ".aiff", ".avi", ".mov", ".mpeg",".qt", ".ram",".viv",".rmvb",".rm",".asf",".divx",".mpg",".mpe",".wmv",".mp4",".mkv",".vob" };

					SortedList
						Model = new SortedList();

					foreach ( var orExt in allowExt ) Model.Add(orExt, orExt);

                    ApplicationHelper.Set("VideoExt", Model);

					return Model;
				}
				else
					return (SortedList)ApplicationHelper.Get("VideoExt");

			}
		}

        /// <summary>
        /// 文件名称
        /// </summary>
        protected static string FileName {
			get {
				Random rd = new Random();
				StringBuilder serial = new StringBuilder();
				serial.Append(DateTime.Now.ToString("ddmmss"));
				serial.Append(rd.Next(10, 10000).ToString());
				return serial.ToString();
			}
		}
	}
}
